package com.lonely.fx.property;

import javafx.beans.property.SimpleSetProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;

/**
 * @date: 2022-07-22 17:01
 * @author: ztkj-hzb
 * @description: 针对 Set 类型的property
 **/
public class SetPropertySettingMain {


    public static void main(String[] args) {


        ObservableSet<User> observableSet = FXCollections.observableSet(new User().setName("aa"), new User().setName("bb"));
        SimpleSetProperty<User> setProperty = new SimpleSetProperty<>(observableSet);

        setProperty.addListener(new ChangeListener<ObservableSet<User>>() {
            @Override
            public void changed(ObservableValue<? extends ObservableSet<User>> observable, ObservableSet<User> oldValue, ObservableSet<User> newValue) {
                System.out.println(observable.hashCode());
                System.out.println(oldValue.hashCode());
                System.out.println(newValue.hashCode());
            }
        });


        setProperty.addListener(new SetChangeListener<User>() {
            @Override
            public void onChanged(Change<? extends User> change) {
                //当前set集合
                ObservableSet<? extends User> users = change.getSet();

                System.out.println("是否是添加操作："+change.wasAdded());
                System.out.println("是否是移除操作："+change.wasRemoved());

                //获取添加的元素
                User elementAdded = change.getElementAdded();
                System.out.println(elementAdded.getName());
            }
        });

        setProperty.add(new User().setName("cc"));

    }


    static class User {
        private String name;

        public String getName() {
            return name;
        }

        public User setName(String name) {
            this.name = name;
            return this;
        }
    }

}
